//Copyright (c) 2010, William Dollins (www.zekiah.com)
//All rights reserved.

//Redistribution and use in source and binary forms, with or 
//without modification, are permitted provided that the following 
//conditions are met:

//Redistributions of source code must retain the above copyright notice, 
//this list of conditions and the following disclaimer.
//Redistributions in binary form must reproduce the above copyright notice, 
//this list of conditions and the following disclaimer in the documentation 
//and/or other materials provided with the distribution.
//Neither the name of Zekiah Technologies, Inc. nor the names of its contributors 
//may be used to endorse or promote products derived from this software without 
//specific prior written permission.

//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
//AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
//ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
//LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
//SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
//CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
//ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
//POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Geometry;
using Zekiah.GeoCommons.ArcGIS.UI;

namespace Zekiah.GeoCommons.ArcGIS.Commands
{
    /// <summary>
    /// Summary description for GCQueryButton.
    /// </summary>
    [Guid("9787eb6b-55c0-4a67-9943-3ed855962e28")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("GeoCommons.GCQueryButton")]
    public sealed class GCQueryButton : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IApplication m_application;
        private GCQueryForm _frm = null;
        public GCQueryButton()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "GeoCommons Tools"; //localizable text
            base.m_caption = "Search GeoCommons";  //localizable text
            base.m_message = "Display GeoCommons search dialog";  //localizable text 
            base.m_toolTip = "Display GeoCommons search dialog";  //localizable text 
            base.m_name = "GeoCommons.GCQueryButton";   //unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                //string bitmapResourceName = "Zekiah.GeoCommons.ArcGIS.Images.gc_logo_16.png"; // GetType().Name + ".png";
                base.m_bitmap = Zekiah.GeoCommons.ArcGIS.Resources.gc_logo_16; // new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overriden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            m_application = hook as IApplication;

            //Disable if it is not ArcMap
            if (hook is IMxApplication)
            {
                base.m_enabled = true;
                _frm = new GCQueryForm(m_application.hWnd, m_application.Document as IMxDocument);
            }
            else
                base.m_enabled = false;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add GCQueryButton.OnClick implementation
            
            _frm.Show();
        }

        public override bool Enabled
        {
            get
            {
                bool enabled = false;
                IMxDocument mxd = m_application.Document as IMxDocument;
                enabled = (mxd.ActiveView.FocusMap.SpatialReference == null) || (mxd.ActiveView.FocusMap.SpatialReference is IUnknownCoordinateSystem);
                return !enabled; // base.Enabled;
            }
        }

        #endregion
    }
}
